package aar.test.tessocr.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.CustomFooterViewCallBack;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import aar.test.tessocr.adapter.OrderListAdapter;
import aar.test.tessocr.baseApplication.BaseApplication;
import aar.test.tessocr.model.DialogCallback;
import aar.test.tessocr.model.LzyResponse;
import aar.test.tessocr.model.UnexporderListModel;
import aar.test.tessocr.utils.ConstantPara;
import aar.test.tessocr.utils.PrefUtils;
import aar.test.tessocr.utils.Urls;
import aar.test.tessocr.view.CustomLoadingMoreFooter;
import butterknife.BindView;
import clx.tessocr.ocreng.R;
import okgo.OkGo;
import okgo.model.Response;
import okhttp3.RequestBody;

import static aar.test.tessocr.Login1Activity.JSON;

/**
 * 已取件列表
 * Created by sunshine on 2020/7/27
 */
public class OrderPickedUpFragment extends BaseFragment implements XRecyclerView.LoadingListener{
    @BindView(R.id.rvData)
    XRecyclerView rvData;
    @BindView(R.id.ll_empty)
    LinearLayout imgEmpty;

    private List<UnexporderListModel> mList = new ArrayList<>();
    private OrderListAdapter mAdapter;
    private boolean isFirst = true;
    private int pageNum = 1;
    private String search = "";

    public static OrderPickedUpFragment newInstance(String orderType, String search) {
        OrderPickedUpFragment fragment = new OrderPickedUpFragment();
        Bundle bundle = new Bundle();
        bundle.putString("orderType", orderType);
        bundle.putString("search", search);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setUpContentView() {
        setContentView(R.layout.fragment_order_delivered);
    }

    @Override
    protected void setUpView(View view) {

    }

    @Override
    protected void setUpComponent() {
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        rvData.setLayoutManager(manager);
        mAdapter = new OrderListAdapter(getActivity(), mList);
        rvData.setAdapter(mAdapter);
        final CustomLoadingMoreFooter footer = new CustomLoadingMoreFooter(getActivity());
        rvData.setFootView(footer, new CustomFooterViewCallBack() {
            @Override
            public void onLoadingMore(View yourFooterView) {
                footer.onLoadingMore();
            }

            @Override
            public void onLoadMoreComplete(View yourFooterView) {
                footer.onLoadMoreComplete();
            }

            @Override
            public void onSetNoMore(View yourFooterView, boolean noMore) {
                footer.onSetNoMore();
            }
        });
        rvData.setPullRefreshEnabled(true);
        rvData.setLoadingMoreEnabled(true);
        rvData.setLoadingListener(this);
    }

    @Override
    protected void setUpData() {

    }

    @Override
    public void onRefresh() {
        pageNum = 1;
        getData();
    }

    @Override
    public void onLoadMore() {
        getData();
    }

    @Override
    public void onResume() {
        super.onResume();
        //recruitType = getArguments().getInt(Constants.RECRUIT_TYPE);
        search = getArguments().getString("search");

        if (isFirst) {
            isFirst = false;
            refreshData();
        }
    }

    private void refreshData() {
        pageNum = 1;
        getData();
    }

    private void getData() {
        if (TextUtils.isEmpty(PrefUtils.getString(getActivity(), "userId", ""))) {
            Toast.makeText(getActivity(), "登录信息为空", Toast.LENGTH_SHORT).show();
            return;

        }

        final Map<String, Object> map = new HashMap<>();
        map.put("adminid", PrefUtils.getString(getActivity(), "userId", ""));
        map.put("keyword", search);
        map.put("status", "4");
        map.put("page", pageNum);
        map.put("size", ConstantPara.PAGESIZE);


        Gson gg = new Gson();
        String json = gg.toJson(map);

        RequestBody body = RequestBody.create(JSON, json);
        OkGo.<LzyResponse<List<UnexporderListModel>>>post(Urls.URL_EXPORDER_LIST)//
                .tag(this)//
                .upRequestBody(body)
                .isMultipart(false)         //强制使用 multipart/form-data 表单上传（只是演示，不需要的话不要设置。默认就是false）
                .execute(new DialogCallback<LzyResponse<List<UnexporderListModel>>>(getActivity()) {
                    @Override
                    public void onSuccess(Response<LzyResponse<List<UnexporderListModel>>> response) {
                        Log.e("OrderListActivity","DeliveredonSuccess--------------------"+response.body().data);
                        if (pageNum == 1) {
                            mList.clear();
                        }

                        mList.addAll(response.body().data);
                        if (null == mList || mList.size() == 0) {
                            imgEmpty.setVisibility(View.VISIBLE);
                        } else {
                            imgEmpty.setVisibility(View.GONE);
                        }

                        if (null == response.body().data || response.body().data.size() == 0) {
                            rvData.refreshComplete();
                            rvData.loadMoreComplete();
                            rvData.setNoMore(true);

                            return;
                        } else {
                            if (null != response.body().data && response.body().data.size() > 0) {
                                rvData.refreshComplete();
                                pageNum++;

                                mAdapter.notifyDataSetChanged();
                            }
                        }
                    }

                    @Override
                    public void onError(Response<LzyResponse<List<UnexporderListModel>>> response) {
                        if (pageNum == 1) {
                            imgEmpty.setVisibility(View.VISIBLE);
                            mList.clear();
                            mAdapter.notifyDataSetChanged();
                        }
                        rvData.refreshComplete();
                        rvData.loadMoreComplete();
                        rvData.setNoMore(true);

                    }
                });
    }
}
